package com.manage.platform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.manage.platform.dto.RoleAllocateMenuDto;
import com.manage.platform.entity.RoleMenuEntity;
import com.manage.platform.mapper.RoleMenuMapper;
import com.manage.platform.utils.TreeListMenuUtil;
import com.manage.platform.vo.MenuPartVo;
import com.manage.platform.vo.MenuTreeVo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * YWD
 * 2025/9/29
 */
@AllArgsConstructor
@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenuEntity> {

    private MenuServiceImpl menuServiceImpl;


    public List<MenuTreeVo> getMenuByRoleId(Long roleId) {
        return this.baseMapper.getMenuTreeByRoleId(roleId);
    }

    public List<MenuTreeVo> getMenuTreeByRoleId(Long roleId) {
        List<MenuTreeVo> list = this.baseMapper.getMenuTreeByRoleId(roleId);
        return TreeListMenuUtil.buildMenuTreeList(list);
    }

    public Boolean allocateMenu(RoleAllocateMenuDto dto) {
        return this.baseMapper.allocateMenu(dto);
    }

    public Boolean initRoleMenu() {
        //菜单列表
        List<Long> list = menuServiceImpl.getMenuIdList();
        //删除再新增
        removeByRoleId(1L);

        RoleAllocateMenuDto menuDto = new RoleAllocateMenuDto();
        menuDto.setRoleId(1L);
        menuDto.setMenuIds(list);
        return this.baseMapper.allocateMenu(menuDto);
    }

    public Boolean removeByRoleId(Long roleId) {
        LambdaUpdateWrapper<RoleMenuEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(RoleMenuEntity::getRoleId, roleId);
        return remove(updateWrapper);
    }

    public List<MenuPartVo> getMenuTreeByRoleIdTwo(Long roleId) {
        return this.baseMapper.getMenuTreeByRoleIdTwo(roleId);
    }

}
